<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Website Thumbnail Class
 * File: webthumbnail_class.php
 * Purpose: Getting thumbnails of websites
 * Todo: Expand feature set
 *
 */

class WebThumbnail {
	private $url = 'http://www.shrinktheweb.com/xino.php?STWAccessKeyId=%s&stwu=%s&stwsize=%s&stwUrl=%s';
	private $access_key;
	private $secret_key;
	
	/**
	* Sets the keys
	*
	* @access private
	*/	
	private function populateKeys() {
		$this->access_key = GlobalSettings::getSetting('stw_access_key');
		$this->secret_key = GlobalSettings::getSetting('stw_secret_key');
	}
	
	/**
	* Obtains a thumbnail
	*
	* @param string $url
	* @param string $size
	* @return string or boolean
	* @access public
	*/	
	public function get_thumbnail($url, $size) {
		// populate the keys
		self::populateKeys();
		$old_f_loc = BASE_PATH . '/storedfiles/webthumbnails/' . md5($url . $size) . '.jpg';
		
		// does the file already exist in the cache?
		if(file_exists($old_f_loc)) {
			// how old is it? if it's older than 30 days, it needs to be regenerated
			if(time() > (filemtime($old_f_loc) + 2592000)) {
				// expired, delete it and recall ourselves
				KernelBaseSystem::removeFile($old_f_loc);
				
				// recall ourselves
				self::get_thumbnail($url, $size);
			} else {
				// does exist, load it straight from there
				// and it hasn't expired
				return 'storedfiles/webthumbnails/' . md5($url . $size) . '.jpg';
			}
		} else {
			// we need to regenerate it
			// is there a key and secret key set?
			if(!empty($this->access_key) && !empty($this->secret_key)) {
				// does have keys
				// put this URL together
				$contents = KernelBaseSystem::fetchWebFile(sprintf($this->url, $this->access_key, $this->secret_key, $size, urlencode($url)));
				
				// valid contents or not?
				if($contents === false) {
					// didn't go too well
					return false;
				} else {
					// got something
					// make it so SimpleXML can read it
					$contents = preg_replace('/stw:/', '', $contents);
					$contents = preg_replace('/xmlns:stw/', 'xmlns', $contents);
				
					try {
						$xml = new SimpleXMLElement($contents, LIBXML_NOCDATA);
					} catch (Exception $e) {
						// unable to parse contents
						trigger_error($e, E_USER_WARNING);
						$failed = true;
					}
					
					// did it work?
					if($failed) {
						// did fail, something went wrong
						return false;
					} else {
						// success, parsed
						// has the thumbnail been returned?
						if($xml->Response->ThumbnailResult->Thumbnail[0]['Exists'] === false) {
							// server has no thumbnail recorded for this
							return false;
						} else {
							// does exist
							// cache it
							if(CacheEngine::CacheImageFile($xml->Response->ThumbnailResult->Thumbnail[0][0], 'storedfiles/webthumbnails', md5($url . $size) . '.jpg', true)) {
								// did work, return new location
								return 'storedfiles/webthumbnails/' . md5($url) . '.jpg';
							} else {
								// didn't work, write problems?
								return false;
							}
						}
					}
				}
			} else {
				// keys aren't set, return false
				return false;
			}
		}
	}
}

?>